<?php

/**
 * This is the model class for table "service_topic".
 *
 * The followings are the available columns in table 'service_topic':
 * @property integer $id
 * @property integer $topic_id
 * @property integer $service_id
 * @property string $description_en
 * @property string $description_th
 * @property string $status
 *
 * The followings are the available model relations:
 * @property Service $service
 * @property Topic $topic
 */
class ServiceTopic extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ServiceTopic the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'service_topic';
	}
	
  public function beforeSave() 
  {
    if($this->isNewRecord)
    {           
      $this->status = 'A';
    }
    return parent::beforeSave();
  }		

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('topic_id, service_id', 'required'),
			array('topic_id, service_id', 'numerical', 'integerOnly'=>true),
			array('status', 'length', 'max'=>45),
			array('description_en, description_th', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, topic_id, service_id, description_en, description_th, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'service' => array(self::BELONGS_TO, 'Service', 'service_id'),
			'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'topic_id' => 'Topic',
			'service_id' => 'Service',
			'description_en' => 'Description En',
			'description_th' => 'Description Th',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('topic_id',$this->topic_id);
		$criteria->compare('service_id',$this->service_id);
		$criteria->compare('description_en',$this->description_en,true);
		$criteria->compare('description_th',$this->description_th,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

  public function getDescription()
  {
    $ret = $this->description_en;

    if (isset($_GET['language'])) {
      switch ($_GET['language']) {
        case "th":
          $data = $this->description_th;
          $ret = (isset($data) && ($data!=''))  ? $data : $ret;
          break;
        default:
          $ret = $this->description_en;
      }
    }

    return $ret;
  }		
	
}